/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	liuyang 
 * 
 * <p>描   述  :   MiddledetectManager.java for com.pengtu.service.business    </p>
 * 
 * <p>最后修改 : $: 2011-9-27-下午1:55:32 v 1.0.0	 liuyang   $     </p>
 * 
 */

package com.pengtu.service.business;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jbpm.api.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.Constants;
import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.business.MiddleDatectionDao;
import com.pengtu.dao.business.MiddleIssueDao;
import com.pengtu.dao.business.MiddlePetitionDao;
import com.pengtu.dao.business.MiddleReportDao;
import com.pengtu.entity.app.User;
import com.pengtu.entity.business.middledetect.MiddleDatection;
import com.pengtu.entity.business.middledetect.MiddleIssue;
import com.pengtu.entity.business.middledetect.MiddlePetition;
import com.pengtu.entity.business.middledetect.MiddleReport;
import com.pengtu.entity.project.ProjectInfo;
import com.pengtu.service.BaseManager;

/**
 * 中间交工验收相关实体的管理类
 * 
 * MiddledetectManager
 * 
 * 2011-9-27 下午1:55:32
 * 
 * @version 1.0.0
 * 
 */
@Component
@Transactional
public class MiddledetectManager extends BaseManager {
	private MiddleDatectionDao middleDatectionDao;
	private MiddleIssueDao middleIssueDao;
	private MiddlePetitionDao middlePetitionDao;
	private MiddleReportDao middleReportDao;

	/**
	 * @param middleIssueDao
	 *            the middleIssueDao to set
	 */
	@Autowired
	public void setMiddleIssueDao(MiddleIssueDao middleIssueDao) {
		this.middleIssueDao = middleIssueDao;
	}

	/**
	 * @param middlePetitionDao
	 *            the middlePetitionDao to set
	 */
	@Autowired
	public void setMiddlePetitionDao(MiddlePetitionDao middlePetitionDao) {
		this.middlePetitionDao = middlePetitionDao;
	}

	/**
	 * @param middleReportDao
	 *            the middleReportDao to set
	 */
	@Autowired
	public void setMiddleReportDao(MiddleReportDao middleReportDao) {
		this.middleReportDao = middleReportDao;
	}

	/**
	 * @param middleDatectionDao the middleDatectionDao to set
	 */
	@Autowired
	public void setMiddleDatectionDao(MiddleDatectionDao middleDatectionDao) {
		this.middleDatectionDao = middleDatectionDao;
	}

	/**
	 * 保存 save: 适用:
	 * 
	 * @param middleDatection
	 * @exception
	 * @since 1.0.0
	 */
	public void save(MiddleDatection middleDatection) {
		middleDatectionDao.save(middleDatection);
	}

	public void save(MiddleIssue middleIssue) {
		middleIssueDao.save(middleIssue);
	}

	public void save(MiddlePetition middlePetition) {
		middlePetitionDao.save(middlePetition);
	}

	public void save(MiddleReport middleReport) {
		middleReportDao.save(middleReport);
	}
	
	public void save(MiddleDatection middleDatection, MiddlePetition middlePetition){
		middleDatectionDao.save(middleDatection);
		middlePetition.setMiddleDatection(middleDatection);
		middlePetitionDao.save(middlePetition);
	}

	/**
	 * 查询 getMiddleDatection: 适用:
	 * 
	 * @param id
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public MiddleDatection getMiddleDatection(String id) {
		return middleDatectionDao.get(id);
	}

	public MiddleIssue getMiddleIssue(String id) {
		return middleIssueDao.get(id);
	}

	public MiddlePetition getMiddlePetition(String id) {
		return middlePetitionDao.get(id);
	}

	public MiddleReport getMiddleReport(String id) {
		return middleReportDao.get(id);
	}
	
	public MiddlePetition findMiddlePetition(MiddleDatection middleDatection){
		return middlePetitionDao.findUniqueBy("middleDatection", middleDatection);
	}
	
	public MiddleReport findMiddleReport(MiddleDatection middleDatection){
		return middleReportDao.findUniqueBy("middleDatection", middleDatection);
	}
	
	public List<MiddleIssue> findIssues(MiddleReport middleReport){
		return middleIssueDao.findBy("middleReport", middleReport);
	}

	/**
	 * 分页查询 searchMiddleDatection: 适用:
	 * 
	 * @param page
	 * @param queryFilter
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public Page<MiddleDatection> searchMiddleDatection(
			final Page<MiddleDatection> page, final QueryFilter queryFilter) {
		return middleDatectionDao.findPage(page, queryFilter);
	}
	
	/**
	 * 待办任务
	 * findPersonalTasks:
	 * 适用:流程处理
	 * @param user
	 * @param projectInfo
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public List<MiddleDatection> findPersonalTasks(User user, ProjectInfo projectInfo) {
		List<Task> taskList = super.findPersonalTasks(user);
		List<MiddleDatection> middleDatections = new ArrayList<MiddleDatection>();
		for (Task task : taskList) {
			String taskId = task.getId();
			String exectionId = task.getExecutionId();
			String arg = exectionId.substring(0, 7);
			if ("mdetect".equals(arg)) {
				String id = (String) super.getVariableByTaskId(taskId,
						Constants.MDETECT_ID);
				MiddleDatection middleDatection = middleDatectionDao.get(id);
				if (middleDatection != null) {
					if (StringUtils.equals(middleDatection.getProjectInfo()
							.getId(), projectInfo.getId())) {
						middleDatection.setTaskId(taskId);
						middleDatections.add(middleDatection);
					}
				}
			}
		}
		return middleDatections;
	}

	public List<MiddleIssue> getIssuesByProId(String projectId) {
		QueryFilter fileter = new QueryFilter();
		fileter.addFilter("projId", projectId);
		return middleIssueDao.find(fileter);
	}

	public void deleteIssue(String id) {
		middleIssueDao.delete(id);
	}
}
